package com.zytlifeservice.controller.pay;

import com.zytlifeservice.domain.Result;
import com.zytlifeservice.repository.pay.ProvinceInfoRepository;
import com.zytlifeservice.service.pay.BroadBandAndPhoneService;
import com.zytlifeservice.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by MJS on 2017/5/31.
 */

@RestController
@RequestMapping(value = "/broadBand")
public class BroadBandAndPhoneController {

    @Autowired
    BroadBandAndPhoneService broadBandAndPhoneService;

    @Autowired
    ProvinceInfoRepository provinceInfoRepository;

    @PostMapping(value = "/queryInfoByAddress")
    public Result queryAddressInfo(@RequestParam("address") String address) {
        List<Map<String, Object>> maps = broadBandAndPhoneService.queryInfoByAddress(address);
        if (maps == null || maps.size() == 0) {
//            return ResultUtil.success(address + "尚未支持该服务", "查询成功");
            return ResultUtil.error(1, address + "尚未支持该服务");
        } else {
            return ResultUtil.success(maps, "查询成功");
        }
    }


    @PostMapping(value = "/preCreate")
    public Result preCreateOrder(@RequestParam("itemId") String itemId,
                                 @RequestParam("account") String account,
                                 @RequestParam("rechargeAmount") String rechargeAmount,
                                 @RequestParam("userId") Long userId,
                                 @RequestParam("address") String address,
                                 @RequestParam("itemName") String itemName) {

        String[] result = broadBandAndPhoneService.createOrder(itemId, account, rechargeAmount, address, itemName, userId);
        if ("success".equals(result[0])) {
            return ResultUtil.success(result[1], "预下单成功");
        } else {
            return ResultUtil.error(202, result[1]);
        }
    }

    @PostMapping(value = "/wxPreCreate")
    public Result preCreateWxOrder(@RequestParam("itemId") String itemId,
                                   @RequestParam("account") String account,
                                   @RequestParam("rechargeAmount") String rechargeAmount,
                                   @RequestParam("userId") Long userId,
                                   @RequestParam("address") String address,
                                   @RequestParam("itemName") String itemName,
                                   @RequestParam("ip") String ip) {
        String[] result = broadBandAndPhoneService.wxPreCreateOrder(itemId, account, rechargeAmount, address, itemName, userId, ip);
        if ("success".equals(result[0])) {
            return ResultUtil.success(result[1], "预下单成功");
        } else {
            return ResultUtil.error(202, result[1]);
        }
    }


    @GetMapping(value = "/queryProvinceInfo")
    public Result queryProvinceInfo() {
        return ResultUtil.success(provinceInfoRepository.findAll(), "查询成功");
    }
}
